package com.wqj.kyee.gps.nav;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.AMapNaviViewOptions;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.wqj.kyee.gps.R;
import com.wqj.kyee.gps.nmea.NMEA;
import com.wqj.kyee.gps.service.NMEAParserService;
import com.wqj.kyee.gps.util.TTSController;

import java.util.ArrayList;

public class RouteNaviActivity extends AppCompatActivity implements AMapNaviListener, AMapNaviViewListener,
                                        NMEAParserService.OnInfoUpdateListener{

    AMapNaviView mAMapNaviView;
    AMapNavi mAMapNavi;
    TTSController mTtsManager;

    String TAG = "RouteNaviActivity";

    private Boolean isServiceBond = false;
    private NMEAParserService mNMEAParserService;
    private final ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            Log.e(TAG, "onServiceConnected");
            mNMEAParserService = ((NMEAParserService.LocalBinder) service).getService();
            if (mNMEAParserService != null) {
                mNMEAParserService.setOnInfoUpdateListener(RouteNaviActivity.this);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "onServiceDisconnected");
            mNMEAParserService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_navi);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().hide();

        mTtsManager = TTSController.getInstance(getApplicationContext());
        mTtsManager.init();

        mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
        mAMapNaviView.onCreate(savedInstanceState);
        mAMapNaviView.setAMapNaviViewListener(this);

        AMapNaviViewOptions options = mAMapNaviView.getViewOptions();
        options.setRouteListButtonShow(true);
        options.setTrafficInfoUpdateEnabled(true);
        options.setTrafficLayerEnabled(true);

        mAMapNaviView.setViewOptions(options);

        mAMapNavi = AMapNavi.getInstance(getApplicationContext());
        mAMapNavi.addAMapNaviListener(this);
        mAMapNavi.addAMapNaviListener(mTtsManager);
        mAMapNavi.setEmulatorNaviSpeed(60);
        //mAMapNavi.startNavi(AMapNavi.EmulatorNaviMode);
        mAMapNavi.setIsUseExtraGPSData(true);
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (!isServiceBond) {
            final Intent intent = new Intent(this, NMEAParserService.class);
            isServiceBond = bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAMapNaviView.onPause();
        //        仅仅是停止你当前在说的这句话，一会到新的路口还是会再说的
        mTtsManager.stopSpeaking();
        //
        //        停止导航之后，会触及底层stop，然后就不会再有回调了，但是讯飞当前还是没有说完的半句话还是会说完
        //        mAMapNavi.stopNavi();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isServiceBond) {
            unbindService(mServiceConnection);
            isServiceBond = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");

        mAMapNaviView.onDestroy();
        mAMapNavi.stopNavi();
        /**
         * 当前页面不销毁AmapNavi对象。
         * 因为可能会返回到RestRouteShowActivity页面再次进行路线选择，然后再次进来导航。
         * 如果销毁了就没办法在上一个页面进行选择路线了。
         * 但是AmapNavi对象始终销毁，那我们就需要在上一个页面用户回退时候销毁了。
         */
        mAMapNavi.removeAMapNaviListener(this);
        mAMapNavi.removeAMapNaviListener(mTtsManager);
//		mAMapNavi.destroy();
        mTtsManager.destroy();

        if (mNMEAParserService != null) {
            mNMEAParserService.setOnInfoUpdateListener(null);
        }
    }

    long startTime = 0l;
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        long currentTime = System.currentTimeMillis();
        if ((currentTime - startTime) >= 2000) {
            Toast.makeText(this, getString(R.string.info_confirm_exit), Toast.LENGTH_SHORT).show();
            startTime = currentTime;
        } else {
            finish();
        }
    }

    @Override
    public void onNaviCancel() {
        this.finish();
    }


    @Override
    public void onRMCUpdate(NMEA.GPSPosition gpsPosition) {
        Location location = new Location("BG-Navi");
        location.setLongitude(gpsPosition.lon);
        location.setLatitude(gpsPosition.lat);
        location.setSpeed(gpsPosition.velocity);
        location.setAccuracy(3f);
        location.setBearing(gpsPosition.dir);
        location.setTime(System.currentTimeMillis());

        Log.e(TAG, "location: " + gpsPosition.toString());

        mAMapNavi.setExtraGPSData(1, location); // 1 for GPS  coordinate , 2 for AMap  coordinate
    }

    @Override
    public void onGGAUpdate(NMEA.GPSPosition position) {

    }

    @Override
    public void onAHRSUpdate(NMEA.GPSPosition position) {

    }

    @Override
    public void onGSVUpdate(ArrayList<NMEA.SVInfo> svList) {

    }

    @Override
    public void onInitNaviFailure() {

    }

    @Override
    public void onInitNaviSuccess() {

    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

    }

    @Override
    public void onGetNavigationText(int i, String s) {

    }

    @Override
    public void onGetNavigationText(String s) {

    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {

    }

    @Override
    public void onCalculateRouteFailure(int i) {

    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }

    @Override
    public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

    }

    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {

    }

    @Override
    public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {

    }

    @Override
    public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showModeCross(AMapModelCross aMapModelCross) {

    }

    @Override
    public void hideModeCross() {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateRouteSuccess(int[] ints) {

    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

    }

    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

    }

    @Override
    public void onPlayRing(int i) {

    }

    @Override
    public void onNaviSetting() {

    }

    @Override
    public boolean onNaviBackClick() {
        return false;
    }

    @Override
    public void onNaviMapMode(int i) {

    }

    @Override
    public void onNaviTurnClick() {

    }

    @Override
    public void onNextRoadClick() {

    }

    @Override
    public void onScanViewButtonClick() {

    }

    @Override
    public void onLockMap(boolean b) {

    }

    @Override
    public void onNaviViewLoaded() {

    }
}
